const app = getApp();

Page({
    data: {
        projectList: [],
        userInfo: null,
        baseURL: '',
        token: ''
    },

    onLoad: function(options) {
        // 获取用户信息
        const userInfo = wx.getStorageSync('userSession');
        const baseURL = wx.getStorageSync('baseURL');
        const token = wx.getStorageSync('token');
        
        if (!userInfo) {
            wx.showToast({
                title: '请先登录',
                icon: 'none'
            });
            return;
        }
        
        if (!baseURL) {
            wx.showToast({
                title: '系统配置错误',
                icon: 'none'
            });
            return;
        }

        if (!token) {
            wx.showToast({
                title: '请重新登录',
                icon: 'none'
            });
            return;
        }
        
        this.setData({
            userInfo: userInfo,
            baseURL: baseURL,
            token: token
        });

        // 获取项目列表
        this.getProjectList();
    },

    onShow: function() {
        // 每次显示页面时刷新项目列表
        this.getProjectList();
    },

    // 获取项目列表
    getProjectList: function() {
        const that = this;
        
        // 构建查询参数
        const params = {
            page: 1,
            limit: 100,
            fuzerenzhanghao: that.data.userInfo.xuehao
        };
        
        wx.request({
            url: that.data.baseURL + '/xiangmuxinxi/page',
            method: 'GET',
            header: {
                'Token': that.data.token
            },
            data: params,
            success: function(res) {
                if (res.data.code === 0) {
                    // 确保只显示当前用户的项目
                    const filteredList = res.data.data.list.filter(item => 
                        item.fuzerenzhanghao === that.data.userInfo.xuehao
                    );
                    
                    that.setData({
                        projectList: filteredList
                    });
                } else {
                    wx.showToast({
                        title: '获取项目列表失败: ' + (res.data.msg || '未知错误'),
                        icon: 'none',
                        duration: 2000
                    });
                }
            },
            fail: function(error) {
                wx.showToast({
                    title: '网络错误: ' + (error.errMsg || '未知错误'),
                    icon: 'none',
                    duration: 2000
                });
            }
        });
    },



    // 跳转到团队页面
    toTeam: function(e) {
        const id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '/pages/xiangmuxinxiguanli/team?id=' + id
        });
    },

    // 删除项目
    deleteProject: function(e) {
        const that = this;
        const id = e.currentTarget.dataset.id;
        
        wx.showModal({
            title: '确认删除',
            content: '确定要删除这个项目吗？',
            success: function(res) {
                if (res.confirm) {
                    wx.request({
                        url: that.data.baseURL + '/xiangmuxinxi/delete',
                        method: 'POST',
                        header: {
                            'Token': that.data.token
                        },
                        data: [id],
                        success: function(res) {
                            if (res.data.code === 0) {
                                wx.showToast({
                                    title: '删除成功',
                                    icon: 'success'
                                });
                                // 刷新项目列表
                                that.getProjectList();
                            } else {
                                wx.showToast({
                                    title: '删除失败: ' + (res.data.msg || '未知错误'),
                                    icon: 'none'
                                });
                            }
                        },
                        fail: function() {
                            wx.showToast({
                                title: '网络错误',
                                icon: 'none'
                            });
                        }
                    });
                }
            }
        });
    }
}); 